package com.xiaoc.bulldozer.controller;

import com.xc.framework.common.page.PageDto;
import com.xc.framework.common.utils.BeanUtil;
import com.xiaoc.bulldozer.common.ResponseCode;
import com.xiaoc.bulldozer.common.dto.AdvertiseDto;
import com.xiaoc.bulldozer.common.dto.AdvertiseQueryDto;
import com.xiaoc.bulldozer.common.model.Advertise;
import com.xiaoc.bulldozer.common.service.AdvertiseService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/share")
public class ShareController extends AdminBaseController {

    public static final Logger logger = LoggerFactory.getLogger(ShareController.class);

    @Resource(name = "advertiseService")
    private AdvertiseService advertiseService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(AdvertiseQueryDto queryDto) {

        if(queryDto.getPage() == null) queryDto.setPage(1);
        queryDto.setType("(1,2,3,4,5,6)");
        int count = advertiseService.getAdvertiseCountByCondition(queryDto);
        
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("pageDto", new PageDto(queryDto.getPage(), count, PageDto.PAGE_SIZE));

        if (count == 0) {
            params.put("queryDto", queryDto);
            return renderHtml("share/list.html", params);
        }

        List<Advertise> advertiseList = advertiseService.getAdvertiseListByCondition(queryDto);
        params.put("advertiseList", advertiseList);
        params.put("queryDto", queryDto);
        return renderHtml("share/list.html", params);
    }

    
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public ModelAndView detail(Integer id) {
        Advertise advertise = null;
        if(id != null) {
            advertise = advertiseService.getAdvertiseById(id);
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("advertise", advertise);
        return renderHtml("share/edit.html", params);
    }

    
    @RequestMapping(value = "/createOrUpdate", method = RequestMethod.POST)
    public ModelAndView createOrUpdate(@RequestBody AdvertiseDto advertiseDto) {

        if (StringUtils.isBlank(advertiseDto.getImgUrl())) {
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "请填写广告图片");
        }

        long currentTime = System.currentTimeMillis();
        Advertise advertise = BeanUtil.copyProperties(Advertise.class, advertiseDto);
        advertise.setUpdateTime(currentTime);

        try {

            if (advertise.getId() == null) {
                advertise.setCreateTime(currentTime);
                advertiseService.save(advertise);
            } else {
                advertiseService.update(advertise);
            }
        } catch (Exception e) {
            logger.error("createOrUpdate failed", e);
            return jsonFailed(ResponseCode.BUSINESS_ERROR.getCode(), "操作失败");
        }
        return jsonSucess();
    }
    
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ModelAndView delete(@RequestBody AdvertiseDto AdvertiseDto) {
        if (AdvertiseDto.getId() == null) {
            return jsonFailed(ResponseCode.PARAM_ERROR_CAN_NOT_NULL.getCode(), "该广告不存在");
        }
        advertiseService.deleteById(AdvertiseDto.getId());
        return jsonSucess();
    }

}
